package com.sg.plmadapter;

import org.eclipse.core.runtime.Platform;

import com.mobnut.commons.Commons;
import com.mobnut.db.DBActivator;
import com.mobnut.db.model.PrimaryObject;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.sg.business.model.Document;
import com.sg.business.model.IModelConstants;
import com.sg.business.model.IPDMServiceProvider;
import com.sg.business.model.IPLM_Object;
import com.sg.plmadapter.adapters.InsertDocument;
import com.sg.plmadapter.adapters.InsertFolder;
import com.sg.plmadapter.adapters.ModifyPhase;
import com.sg.plmadapter.adapters.MoveDocument;
import com.sg.plmadapter.adapters.RemoveDocument;
import com.sg.plmadapter.adapters.RemoveFolder;
import com.sg.plmadapter.adapters.RenameFolder;
import com.sg.plmadapter.adapters.SetLifeCycleStatus;
import com.sg.plmadapter.adapters.UpdateDocument;
import com.sg.plmadapter.adapters.UpdateDocumentVersion;
import com.sg.plmadapter.adapters.WindchillService;
import com.sg.plmadapter.adapters.WindchillSyncJob;
import com.sg.plmadapter.model.PLMPrimaryObject;
import com.sg.plmadapter.windchill.PMWebservice;

public class PLMService {

	private DBCollection plmSyncCol;
	protected boolean hasReloadSyncPLM = false;

	private DBCollection getCol(String collectionName) {
		return DBActivator.getCollection(IModelConstants.DB, collectionName);
	}

	public void stop() {

	}

	public void strat() {
		plmSyncCol = getCol(PLMAdapter.C_PLM_SYNC);
	}

	public void doFinishSyncPLM(PrimaryObject po, String requestCode, long time)
			throws Exception {
		DBCollection col = po.getCollection();
		col.update(po.queryThis(), new BasicDBObject().append("$set",
				new BasicDBObject().append(IPLM_Object.F_SYNC_REQUEST, null)));
		if (requestCode != null && !"".equals(requestCode)) {
			PLMPrimaryObject plmpo = new PLMPrimaryObject(po, requestCode, time);
			plmpo.doRemove();
		}

		// doReloadSyncPLM();
	}

	public void doStartSyncPLM(PrimaryObject po, String requestCode, long time)
			throws Exception {
		DBCollection col = po.getCollection();
		col.update(po.queryThis(), new BasicDBObject().append("$set",
				new BasicDBObject().append(IPLM_Object.F_SYNC_REQUEST,
						requestCode)));

		if (requestCode != null && !"".equals(requestCode)) {
			PLMPrimaryObject plmpo = new PLMPrimaryObject(po, requestCode, time);
			plmpo.doInsert();
		}
	}

	public void doReloadSyncPLM() throws Exception {
		if (!hasReloadSyncPLM) {
			Thread thread = new Thread(new Runnable() {

				@Override
				public void run() {
					try {
						hasReloadSyncPLM = true;

						DBCursor find = plmSyncCol.find();
						find.sort(new BasicDBObject().append(
								PLMPrimaryObject.F_PLM_SYNCDATE, 1));
						while (find.hasNext()) {
							DBObject dbo = find.next();
							PLMPrimaryObject plmpo = new PLMPrimaryObject(dbo);
							PrimaryObject po = plmpo.getPrimaryObject();
							if (po == null) {
								plmpo.doRemove();
							} else {
								String requestCode = plmpo.getRequestCode();
								long time = plmpo.getTime();
								doSyncPLM(po, requestCode);
								doFinishSyncPLM(po, requestCode, time);
							}
						}
						find.close();
					} catch (Exception e) {
						Commons.logerror("PLMService doReloadSyncPLM run Error:"
								+ e.getMessage());
					} finally {
						hasReloadSyncPLM = false;
					}
				}
			});
			thread.setDaemon(true);
			thread.start();
		}
	}

	public void doSyncPLM(PrimaryObject po, String requestCode)
			throws Exception {
		IPDMServiceProvider pdmService = getPDMServiceAdapter(po);
		if (pdmService instanceof WindchillService) {
			WindchillService service = (WindchillService) pdmService;
			PMWebservice windchill = service.getWindchill();
			WindchillSyncJob job = null;
			if (IPLM_Object.REQUEST_CHANGE_PHASE.equals(requestCode)) {
				job = new ModifyPhase(windchill, po);
			} else if (IPLM_Object.REQUEST_CHANGE_REV.equals(requestCode)) {
				job = new UpdateDocumentVersion(windchill, po);
			} else if (IPLM_Object.REQUEST_INSERT_DOCUMENT.equals(requestCode)) {
				job = new InsertDocument(windchill, po);
			} else if (IPLM_Object.REQUEST_INSERT_FOLDER.equals(requestCode)) {
				job = new InsertFolder(windchill, po);
			} else if (IPLM_Object.REQUEST_MOVE.equals(requestCode)) {
				job = new MoveDocument(windchill, po);
			} else if (IPLM_Object.REQUEST_REMOVE_ALL.equals(requestCode)) {
				String type = (String) po.getValue(WindchillService.F_PLM_TYPE);
				String plmType = service.changeToPLMType(type);
				job = new RemoveDocument(windchill, po,
						Document.REMOVE_TYPE_ALL, plmType);
			} else if (IPLM_Object.REQUEST_REMOVE_FOLDER.equals(requestCode)) {
				job = new RemoveFolder(windchill, po);
			} else if (IPLM_Object.REQUEST_REMOVE_MV.equals(requestCode)) {
				String type = (String) po.getValue(WindchillService.F_PLM_TYPE);
				String plmType = service.changeToPLMType(type);
				job = new RemoveDocument(windchill, po,
						Document.REMOVE_TYPE_MV, plmType);
			} else if (IPLM_Object.REQUEST_REMOVE_SV.equals(requestCode)) {
				String type = (String) po.getValue(WindchillService.F_PLM_TYPE);
				String plmType = service.changeToPLMType(type);
				job = new RemoveDocument(windchill, po,
						Document.REMOVE_TYPE_SV, plmType);
			} else if (IPLM_Object.REQUEST_RENAME_FOLDER.equals(requestCode)) {
				job = new RenameFolder(windchill, po);
			} else if (IPLM_Object.REQUEST_SETLIFECYCLE.equals(requestCode)) {
				job = new SetLifeCycleStatus(windchill, po);
			} else if (IPLM_Object.REQUEST_UPDATE_DOCUMENT.equals(requestCode)) {
				job = new UpdateDocument(windchill, po);
			}
			if (job != null) {
				job.doReload();
			}
		}
	}

	private IPDMServiceProvider getPDMServiceAdapter(PrimaryObject po) {
		Object object = Platform.getAdapterManager().getAdapter(po,
				IPDMServiceProvider.class);
		if (object instanceof IPDMServiceProvider) {
			return (IPDMServiceProvider) object;
		}
		return null;
	}
}
